package ru.spb.osll.myfit.client.widget;

import ru.spb.osll.myfit.client.PageMode;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;
import ru.spb.osll.myfit.common.WikiText;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class WikiTextWidget extends PageElementWidget {

  private WikiText m_wikiText;

  public WikiTextWidget(GWTWidgetFactory gwtWidgetFactory, WikiText wikiText) {
    super(gwtWidgetFactory);
    m_wikiText = wikiText;

    sinkEvents(Event.ONMOUSEUP | Event.ONDBLCLICK | Event.ONCONTEXTMENU);
    createPopupMenu();
    initWidget(createContentWidget());
    m_mode = PageMode.EDIT;
  }

  private Widget createContentWidget() {
    VerticalPanel panel = m_gwtWidgetFactory.createVerticalPanel();
    panel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
    HTML html = m_gwtWidgetFactory.createHTML(m_wikiText.getHtmlText());
    panel.add(html);
    return panel;
  }

  @Override
  public PageElement getPageElement() {
    return m_wikiText;
  }

  @Override
  public void setOpen(boolean isOpen) {

  }

  @Override
  public PageElementType getType() {
    return PageElementType.WIKI_TEXT;
  }
}
